/**
 * Created by yindume on 2018/12/10.
 */
var express = require('express');
var router = express.Router();
var db = require('../../../libs/mysql');
//统一返回格式
var res_Data;
router.use( function(req, res, next) {
    res_Data = {
        code: 0,
        message: ''
    }
    next();
});
router.get('/',function (req,res) {

    var page = 1; //默认为1
    var num = 10; //一页条数
    if (req.query.page) {
        page = parseInt(req.query.page);
    }
    var count = 0;
    var pages = 0;

    db.query(`select count(*) from user;`,(err,data)=>{
        if(err){
            res.status(500).send('用户总页数').end();
        }else {
            count = data[0]['count(*)'];
            pages = Math.ceil(count/num);//总页数
            var str = 'SELECT id,YD,nickname,isAdmin FROM user limit ' + num + ' offset ' + num * (page - 1);
            db.query(str,(err,data)=>{
                if(err){
                    res.status(500).send('没有上页').end();
                }else {
                    if (!data[0]) {
                        return;
                    }
                    res.render('admin/user/user', {
                        userInfo:req.userInfo,
                        count:count,
                        pages:pages,
                        page: page,
                        num:num,
                        userList: data
                    });
                }
            });

        }
    })
});


router.post('/set_admin',function(req,res){
    var setadminid = req.body.setadminid;
    db.query(`UPDATE user SET isAdmin = '1' WHERE id = '${setadminid}';`,(err,data)=>{
        if(err){
            res.status(500).send('用户列表查询').end();
        }else {
            res_Data.message = '设置成功';
            res.json(res_Data);
            return;
        }
    });
})

module.exports = router;